<template>
	<!-- 问题反馈 -->
	<view class="feedback_wp">
		<view class="f_info">
			<view class="f_title">反馈内容</view>
			<textarea class="f_textarea" v-model="content" placeholder="请输入你要反馈的内容" />
		</view>
		<view class="f_info">
			<view class="f_title">上传图片</view>
			<view class="f_images">
				<view class="f_item" v-for="(itme,index) in img" :key="index">
					<image :src="itme" ></image>
					<text @click="deleteImg(index)">×</text>
				</view>
				<view class="f_item">
					<image v-if="loading" class="f_loading" src="/static/business/loading.gif"></image>
					<image @click="uploadImage" class="add_image" src="/static/shizi.jpg" ></image>
				</view>
			</view>
		</view>
		<view class="f_buttons" @click="onsubmit">提交</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				content: '',
				stopIndex: 0,
				img: [],
				loading: false
			}
		},
		methods: {
			onsubmit() {
				if (this.content == '') {
					this.$api.msg('请填写反馈内容！');
					return;
				}
				let obj = {
					content: this.content,
					img: this.img
				}
				this.$axios('apply/feedback', 'POST', 'salemsg', obj).then(res => {
					if (res.data.code == 200) {
						this.$api.msg('提交成功！');
						setTimeout(() => {
							this.$navTo('/pages/individualism/user');
						}, 1000)
					}
				})
			},
			deleteImg(v){
				this.img.splice(v,1);
			},
			uploadImage() {
				var that = this;
				uni.chooseImage({
					success: function(res) {
						if (that.loading) return;
						that.loading = true;
						that.stopIndex = res.tempFilePaths.length;
						if (that.stopIndex <= 0) {
							that.loading = false;
						}
						res.tempFilePaths.map(value => {
							that.upload(value);
						})
					}
				});
			},
			upload(filePath) {
				var that = this;
				let token = uni.getStorageSync('token');
				uni.uploadFile({
					url: that.$url.state.url.upload,
					filePath,
					name: 'a',
					header: {
						token: token
					},
					formData: {
						'file': 'a'
					},
					success: upres => {
						let data = JSON.parse(upres.data);
						if (data.code = 200) {
							that.img.push(data.data.url);
							that.stopIndex--;
							if(that.stopIndex<=0){
								that.loading = false;
							}
						} else {
							that.$api.msg('上传失败请重试！')
						}
					},
					fail() {
						that.$api.msg('上传失败请重试！')
					}
				})
			}
		}
	}
</script>

<style lang="scss">
	.feedback_wp {
		color: $font-color-dark;
		padding: 0 20upx 130upx;
	}

	.f_info {
		margin-bottom: 40upx;
	}

	.f_title {
		line-height: 60upx;
	}

	.f_textarea {
		background: #f2f2f2;
		border-radius: 10upx;
		color: $font-color-dark;
		line-height: 40upx;
		padding: 20upx;
		font-size: $font-sm;
		width: 100%;
	}

	.f_images {
		position: relative;
		display: flex;
		align-items: center;
		flex-wrap: wrap;
	}

	.f_item {
		position: relative;
		margin: 0 30upx 20upx 0;
	}

	.f_item image {
		border-radius: 10upx;
		height: 142upx;
		width: 142upx;
	}

	.f_item text {
		background: $base-color;
		border-radius: 50%;
		color: #fff;
		position: absolute;
		top: -10upx;
		right: -10upx;
		height: 40upx;
		width: 40upx;
		font-size: 36upx;
		text-align: center;
		line-height: 40upx;
		z-index: 10;
	}

	.f_images .f_loading {
		position: absolute;
		top: 41upx;
		left: 41upx;
		width: 60upx;
		height: 60upx;
		z-index: 100;
	}

	.add_image {
		border: 1px hsl(0, 0%, 87%) solid;
	}

	.f_buttons {
		background: linear-gradient(to right, #ff7101, #f03201);
		border-radius: 40upx;
		color: #fff;
		line-height: 70upx;
		letter-spacing: 2px;
		margin: 0 auto;
		text-align: center;
		width: 90%;
	}
</style>
